﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.DomainServices.Client;
using System.Windows.Controls;
using System.Windows.Data;
using GSSoftSL.Utility;
using GSSoftSL.Web;
using GSSoftSL.Web.Models;
using GSSoftSL.Web.Services;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Ioc;
using Microsoft.Practices.ServiceLocation;

namespace GSSoftSL.ViewModel.HocVienVM
{
    public class HocVienMainViewModel : ViewModelBase
    {
        private GSSoftContext _dc;
        public GSSoftContext DC
        {
            get { return _dc; }
        }

        /*public HocVienEditViewModel HocVienEditViewModel
        {
            get { return ServiceLocator.Current.GetInstance<HocVienEditViewModel>("HocVien"); }
        }*/
        public HocVienKhoaHocViewModel HocVienKhoaHocViewModel
        {
            get { return ServiceLocator.Current.GetInstance<HocVienKhoaHocViewModel>("HocVien"); }
        }
        public HocVienHocPhiViewModel HocVienHocPhiViewModel
        {
            get { return ServiceLocator.Current.GetInstance<HocVienHocPhiViewModel>("HocVien"); }
        }

        /// <summary>
        /// Initializes a new instance of the HocVienViewModel class.
        /// </summary>
        public HocVienMainViewModel()
        {
            _dc = ServiceLocator.Current.GetInstance<DomainContext>("HocVien") as GSSoftContext;
            _dc.Load<ThongTin>(_dc.GetThongTinsQuery(), LoadBehavior.RefreshCurrent, LoadThongTin_Completed, null);
            _dc.Load<Truong>(_dc.GetTruongsQuery(), LoadBehavior.RefreshCurrent, LoadTruong_Completed, null);
        }

        private void LoadThongTin_Completed(LoadOperation<ThongTin> load)
        {
            ThongTins = load.Entities.ToArray();
        }

        private bool _isBusy = false;
        public bool IsBusy
        {
            get
            {
                return _isBusy;
            }
            set
            {
                _isBusy = value;
                RaisePropertyChanged("IsBusy");
            }
        }

        private void LoadTruong_Completed(LoadOperation<Truong> loadTruong)
        {
            Truongs = loadTruong.Entities;
            IsBusy = false;
        }

        private HocVien _hocVien;
        public HocVien HocVien
        {
            get
            {
                return _hocVien;
            }
            set
            {
                if (_hocVien == value)
                {
                    return;
                }
                _hocVien = value;
                RaisePropertyChanged("HocVien");
            }
        }

        private ThongTin[] _thongTins = null;
        public ThongTin[] ThongTins
        {
            get
            {
                return _thongTins;
            }
            set
            {
                if (_thongTins == value)
                {
                    return;
                }
                _thongTins = value;
                RaisePropertyChanged("ThongTins");
            }
        }

        private IEnumerable<Truong> _truongs = null;
        public IEnumerable<Truong> Truongs
        {
            get
            {
                return _truongs;
            }
            set
            {
                if (_truongs == value)
                {
                    return;
                }
                _truongs = value;
                RaisePropertyChanged("Truongs");
            }
        }

        private RelayCommand _newHocVienCommand;
        public RelayCommand NewHocVienCommand
        {
            get
            {
                return _newHocVienCommand
                       ?? (_newHocVienCommand = new RelayCommand(
                                                    () =>
                                                    {
                                                        //HocVienEditViewModel.HocVien = null;
                                                        //HocVienEditViewModel.HocVien = new HocVien() {MaHV = ""};
                                                        if (_dc.HasChanges)
                                                            _dc.RejectChanges();
                                                        HocVien = new HocVien() { MaHV = "" };
                                                        SetStatus(EnumAction.New);
                                                    },
                                                    () => true));
            }
        }

        private RelayCommand _saveCommand;
        public RelayCommand SaveCommand
        {
            get
            {
                return _saveCommand
                       ?? (_saveCommand = new RelayCommand(
                                              () =>
                                              {
                                                  if (_hocVien == null || _dc.IsSubmitting)
                                                      return;
                                                  if (_hocVien.HoLot == null || _hocVien.HoLot.Trim() == "")
                                                  {
                                                      MessageWindow.CreateNew("Họ học viên không được để trống.");
                                                      return;
                                                  }
                                                  if (_hocVien.Ten == null || _hocVien.Ten.Trim() == "")
                                                  {
                                                      MessageWindow.CreateNew("Họ học viên không được để trống.");
                                                      return;
                                                  }
                                                  ConfirmWindow.CreateNew("Bạn có muốn lưu?", Save);
                                                  SetStatus(EnumAction.Save);
                                              },
                                              () => true/*!(_hocVien == null || _dc.IsSubmitting)*/));
            }
        }

        public void Save()
        {
            if (_hocVien.EntityState == EntityState.New || _hocVien.EntityState == EntityState.Detached)
            {
                _hocVien.NguoiTao = _hocVien.NguoiCapNhat = UtilityFunction.GetCurrentUserId();
                _hocVien.NgayTao = _hocVien.NgayCapNhat = DateTime.Now;
                _dc.HocViens.Add(_hocVien);
            }
            else if (_hocVien.EntityState == EntityState.Modified)
            {
                _hocVien.NguoiCapNhat = UtilityFunction.GetCurrentUserId();
                _hocVien.NgayCapNhat = DateTime.Now;
                //_dc.HocViens.Attach(_hocVien);
            }
            IsBusy = true;
            _dc.SubmitChanges(Save_Completed, null);
        }

        private void Save_Completed(SubmitOperation obj)
        {
            IsBusy = false;
            //RaisePropertyChanged("HocVien");
        }

        private RelayCommand _editCommand;
        public RelayCommand EditCommand
        {
            get
            {
                return _editCommand
                    ?? (_editCommand = new RelayCommand(
                                          () =>
                                          {
                                              if (_hocVien == null || _hocVien.EntityState == EntityState.New || _hocVien.EntityState == EntityState.Detached)
                                                  return;
                                              SetStatus(EnumAction.Edit);
                                          }));
            }
        }

        private RelayCommand _cancelCommand;
        public RelayCommand CancelCommand
        {
            get
            {
                return _cancelCommand
                    ?? (_cancelCommand = new RelayCommand(
                                          () =>
                                          {
                                              SetStatus(EnumAction.Cancel);
                                              if (_hocVien.EntityState == EntityState.New || _hocVien.EntityState == EntityState.Detached)
                                                  HocVien = null;
                                              if (_dc.HasChanges)
                                                  _dc.RejectChanges();
                                          }));
            }
        }
        
        private DataFormMode _mode = DataFormMode.ReadOnly;
        public DataFormMode Mode
        {
            get
            {
                return _mode;
            }
            set
            {
                if (_mode == value)
                {
                    return;
                }
                _mode = value;
                RaisePropertyChanged("Mode");
            }
        }

        private bool _isEditable = false;
        public bool IsEditable
        {
            get
            {
                return _isEditable;
            }
            set
            {
                if (_isEditable == value)
                {
                    return;
                }
                _isEditable = value;
                RaisePropertyChanged("IsEditable");
            }
        }
        
        private bool _canEdit = false;
        public bool CanEdit
        {
            get
            {
                return _canEdit;
            }
            set
            {
                if (_canEdit == value)
                {
                    return;
                }
                _canEdit = value;
                RaisePropertyChanged("CanEdit");
            }
        }
        
        private bool _canSave = false;
        public bool CanSave
        {
            get
            {
                return _canSave;
            }
            set
            {
                if (_canSave == value)
                {
                    return;
                }
                _canSave = value;
                RaisePropertyChanged("CanSave");
            }
        }
        
        private bool _canCancel = false;
        public bool CanCancel
        {
            get
            {
                return _canCancel;
            }
            set
            {
                if (_canCancel == value)
                {
                    return;
                }
                _canCancel = value;
                RaisePropertyChanged("CanCancel");
            }
        }
        
        private bool _canDetail = true;
        public bool CanDetail
        {
            get
            {
                return _canDetail;
            }
            set
            {
                if (_canDetail == value)
                {
                    return;
                }
                _canDetail = value;
                RaisePropertyChanged("CanDetail");
            }
        }
        
        private bool _canDoBusiness = false;
        public bool CanDoBusiness
        {
            get
            {
                return _canDoBusiness;
            }
            set
            {
                if (_canDoBusiness == value)
                {
                    return;
                }
                _canDoBusiness = value;
                RaisePropertyChanged("CanDoBusiness");
            }
        }

        public void SetStatus(EnumAction Action)
        {
            switch(Action)
            {
                case EnumAction.New:
                    IsEditable = true;
                    CanSave = true;
                    CanCancel = true;
                    CanDoBusiness = false;
                    CanEdit = false;
                    break;
                case EnumAction.Save:
                    IsEditable = false;
                    CanSave = false;
                    CanCancel = false;
                    CanDoBusiness = true;
                    CanEdit = true;
                    break;
                case EnumAction.Edit:
                    IsEditable = true;
                    CanSave = true;
                    CanCancel = true;
                    CanDoBusiness = false;
                    CanEdit = false;
                    break;
                case EnumAction.Cancel:
                    IsEditable = false;
                    CanSave = false;
                    CanCancel = false;
                    if (_hocVien.EntityState == EntityState.Modified || _hocVien.EntityState == EntityState.Unmodified)
                    {
                        CanDoBusiness = true;
                        CanEdit = true;
                    }
                    break;
                case EnumAction.Detail:
                    IsEditable = false;
                    CanSave = false;
                    CanCancel = false;
                    CanDoBusiness = true;
                    CanEdit = true;
                    break;
            }
        }
    }

    public class EnableConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value == null || value.ToString().Trim() == "";
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
}